<template>
  <div>
    <h4>成品不合格信息：</h4>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      class="j-table-force-nowrap"
      :scroll="{ x: true }"
      :columns="conformanceColumns"
      :dataSource="finishedList"
    >
      <template slot="action" slot-scope="text, record">
        <a @click="conformanceDetail(record)">详情</a>
      </template>
    </a-table>
    <h4>材料不合格信息：</h4>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      class="j-table-force-nowrap"
      :scroll="{ x: true }"
      :columns="conformanceColumns"
      :dataSource="materialList"
    >
      <template slot="action" slot-scope="text, record">
        <a @click="conformanceDetail(record)">详情</a>
      </template>
    </a-table>
    <non-conformance-modal ref="conformanceForm" />
  </div>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { getAction } from '@api/manage'
import NonConformanceModal from '@/views/qms/non/modules/NonConformanceModal'

export default {
  name: 'conformance',
  components: { NonConformanceModal },
  data() {
    return {
      description: '检验信息',
      finishedList: [],
      materialList: [],
      conformanceColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          fixed: 'left',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '单据类型',
          align: 'center',
          dataIndex: 'documentType_dictText',
          width: 80,
          fixed: 'left',
        },
        {
          title: '单据号',
          align: 'center',
          dataIndex: 'documentNumber',
          width: 80,
          fixed: 'left',
        },
        {
          title: '不合格单号',
          align: 'center',
          dataIndex: 'nonConformanceNumber',
          width: 80,
          fixed: 'left',
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode',
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName',
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'specificationModel',
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText',
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'quantity',
        },
        {
          title: '批次号',
          align: 'center',
          dataIndex: 'batchNumber',
        },
        {
          title: '物料批号',
          align: 'center',
          dataIndex: 'materialBatchNumber',
        },
        {
          title: '储位',
          align: 'center',
          dataIndex: 'storageLocation_dictText',
        },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'entryDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '生产日期',
          align: 'center',
          dataIndex: 'productionDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '保质期（天）',
          align: 'center',
          dataIndex: 'shelfLife',
        },
        {
          title: '限用日期',
          align: 'center',
          dataIndex: 'expirationDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '处理结果',
          align: 'center',
          dataIndex: 'processingOpinion',
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy_dictText',
        },
        {
          title: '创建部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText',
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 70,
          scopedSlots: { customRender: 'action' },
        },
      ],
    }
  },

  methods: {
    loadConformanceData(batchNumber) {
      this.finishedList = []
      this.materialList = []
      if (batchNumber) {
        getAction('/qms/non-conformance/finished-product/list', {
          batchNumber: batchNumber,
          nonConformanceType: 'finished-product',
          pageNo: 1,
          pageSize: 100,
        }).then((res) => {
          if (res.success) {
            this.finishedList = res.result.records || res.result || []
          }
        })
        getAction('/qms/non-conformance/material/list', {
          batchNumber: batchNumber,
          nonConformanceType: 'material',
          pageNo: 1,
          pageSize: 100,
        }).then((res) => {
          if (res.success) {
            this.materialList = res.result.records || res.result || []
          }
        })
      }
    },
    conformanceDetail: function (record) {
      this.$refs.conformanceForm.detail(record)
      this.$refs.conformanceForm.title = '详情'
      this.$refs.conformanceForm.disableSubmit = true
      this.$refs.conformanceForm.nonConformanceType = record.nonConformanceType
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
::v-deep .ant-table-tbody tr {
  height: 36px !important;
}
</style>
